package com.huangmb.anymark.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.activity.base.AppBaseActivity;
import com.huangmb.anymark.app.fragment.marker.FoodShopFragment;
import com.huangmb.anymark.app.fragment.marker.SceneryFragment;
import com.huangmb.anymark.app.fragment.marker.TravelNoteFragment;
import com.huangmb.anymark.app.model.UserMarker;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户标记的详情页
 *
 * @author hmb
 * @version 1.0
 * @since 2016/4/24 10:32
 */
public class MarkerDetailActivity extends AppBaseActivity {
    static Map<String, Class<? extends Fragment>> fragmentMap = new HashMap<>();

    private Toolbar mToolbar;

    static {
        fragmentMap.put(UserMarker.TYPE_STORE, FoodShopFragment.class);
        fragmentMap.put(UserMarker.TYPE_SCHEDULE, TravelNoteFragment.class);
        fragmentMap.put(UserMarker.TYPE_SCENERY, SceneryFragment.class);
    }

    public static void newInstance(Context context, UserMarker marker) {
        Intent intent = new Intent(context, MarkerDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle b = new Bundle();
        b.putParcelable("marker", marker);
        intent.putExtras(b);
        context.startActivity(intent);
    }

    @Override
    protected void onInitView(Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);
        setContentView(R.layout.activity_marker_detail);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void afterInitView(Bundle savedInstanceState) {
        super.afterInitView(savedInstanceState);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        UserMarker marker = extras.getParcelable("marker");
        if (marker == null) {
            toast("未提供有效参数");
            finish();
            return;
        }
        String title = marker.getTitle();

        if (title != null) {
            getSupportActionBar().setTitle(title);
        }


        try {
            Fragment fragment = new FoodShopFragment();

            findViewById(R.id.loading).setVisibility(View.GONE);//隐藏进度条

            Bundle bundle = new Bundle();
            bundle.putParcelable("marker", marker);
            fragment.setArguments(bundle);


            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();

        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }

    }
}
